Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Lucene.Net.Index
Imports Lucene.Net.Search

Namespace Aricie.DNN.Modules.LuceneSearch.UI
    Partial Public Class QueryParserControl
        Inherits LuceneFilterControlBase

#Region "Private Attributes"
        Private searchCriteria As String = ""

#End Region


#Region "Event Handlers"

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

            If Not Me.IsPostBack Then
                searchCriteria = BusinessController.HttpCriteriaSring

                Me.BindData()
            End If
        End Sub


        Protected Sub ctlFilters_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) _
            Handles ctlFilters.ItemDataBound
            Select Case e.Item.ItemType
                Case ListItemType.Item, ListItemType.AlternatingItem
                    Dim _
                        objData As KeyValuePair(Of ListItem, Query) = _
                            DirectCast(e.Item.DataItem, KeyValuePair(Of ListItem, Query))
                    Dim lblField As Label = CType(e.Item.FindControl("lblFilterField"), Label)
                    Dim lblValue As Label = CType(e.Item.FindControl("lblFilterValue"), Label)
                    Dim lnkFilter As HyperLink = CType(e.Item.FindControl("lnkTakeOutFilter"), HyperLink)
                    lblField.Text = objData.Key.Text
                    lblValue.Text = objData.Key.Value
                    lnkFilter.NavigateUrl = _
                        BusinessController.GetSearchUrl(Me.ParentModuleBase.TabId, Me.searchCriteria, _
                                                         objData.Value.ToString)

            End Select


        End Sub

#End Region


#Region "ILuceneFilterControl implements"

        Public Overrides Property Enabled() As Boolean
            Get
                Return Me.ctlFilters.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.ctlFilters.Enabled = value
            End Set
        End Property

        Public Overrides Function GetFilter() As LuceneFilterQuery
            Dim toReturn As New LuceneFilterQuery
            toReturn.FilterQuery = BusinessController.GetHttpFilterQuery
            Return toReturn

        End Function

        Public Overrides Function HasFilter() As Boolean
            Return BusinessController.GetHttpFilterQuery IsNot Nothing
        End Function

        Public Overrides Function GetFieldNames() As IEnumerable(Of String)
            Return New String() {}

        End Function


        Public Overrides Function ParseFilter(ByVal objFilter As LuceneFilterQuery) As Boolean

            Return BindFilter(objFilter.FilterQuery)
        End Function


#End Region

#Region "Private methods"


        Private Function BindFilter(ByVal objFilter As Query) As Boolean

            Dim dico As Dictionary(Of ListItem, Query) = PrepareFilter(objFilter, Nothing)
            Me.ctlFilters.DataSource = dico
            Me.ctlFilters.DataBind()
            Return dico.Count > 0

        End Function


        Private Function PrepareFilter(ByVal objFilter As Query, ByVal removedQuery As Query) _
            As Dictionary(Of ListItem, Query)

            Dim toReturn As New Dictionary(Of ListItem, Query)
            If objFilter IsNot Nothing Then
                If TypeOf objFilter Is BooleanQuery Then

                    Dim boolQuery As BooleanQuery = DirectCast(objFilter, BooleanQuery)
                    For Each objClause As BooleanClause In boolQuery.GetClauses
                        Dim clausequery As Query = objClause.Query
                        Dim newRemovedQuery As New BooleanQuery
                        For Each objNewClause As BooleanClause In boolQuery.GetClauses
                            Dim newClauseQuery As Query = objNewClause.Query()
                            If Not newClauseQuery.ToString() = clausequery.ToString() Then
                                newRemovedQuery.Add(objNewClause)
                            End If
                        Next
                        Dim tempDico As Dictionary(Of ListItem, Query) = PrepareFilter(clausequery, newRemovedQuery)
                        For Each key As ListItem In tempDico.Keys
                            toReturn(key) = tempDico(key)
                        Next
                    Next
                ElseIf TypeOf objFilter Is TermQuery Then
                    Dim objTerm As Term = DirectCast(objFilter, TermQuery).Term()
                    If objTerm.Field = DnnSearchItemField.ModuleName Then
                        Dim termValue As String = BusinessController.GetTermText(objTerm)
                        If removedQuery Is Nothing Then
                            toReturn.Add(New ListItem(objTerm.Field, termValue), objFilter)
                        Else
                            toReturn.Add(New ListItem(objTerm.Field, termValue), removedQuery)
                        End If
                    End If

                ElseIf TypeOf objFilter Is PhraseQuery Then
                    Dim objTerms() As Term = DirectCast(objFilter, PhraseQuery).GetTerms()
                    If objTerms.Length > 0 Then
                        Dim item As New ListItem(objTerms(0).Field, """")
                        For Each objterm As Term In objTerms
                            item.Value &= BusinessController.GetTermText(objterm)
                        Next
                        item.Value &= """"
                        toReturn.Add(item, removedQuery)
                    End If


                End If
            End If

            Return toReturn

        End Function


        Private Sub BindData()

            Dim filterQuery As New LuceneFilterQuery
            filterQuery.FilterQuery = BusinessController.GetHttpFilterQuery
            If Not Me.ParseFilter(filterQuery) Then
                Me.Visible = False
            Else
                Me.Visible = True
            End If


        End Sub

#End Region
    End Class
End Namespace
